// /////////////////////////////////////////////////////////////////////////////
//
// Name:            ColourScript.cpp
// Author:          Michael Bartsch
//
// Desc:            This file contains the Glue functions that enable the
//                  interaction between CColourAnalyser and Lua.
//
// License:         Copyright (C) 2008-2009 Michael Bartsch
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the GNU General Public
//                  License as published by the Free Software Foundation,
//                  either version 3 of the License, or (at your option) any
//                  later version.
//
//                  This program is distributed in the hope that it will be
//                  useful, but WITHOUT ANY WARRANTY; without even the implied
//                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//                  PURPOSE. See the GNU General Public License for more
//                  details.
//
//                  You should have received a copy of the GNU General Public
//                  License along with this program. If not, see
//                  <http://www.gnu.org/licenses/>.
//
// /////////////////////////////////////////////////////////////////////////////

// Include required header file(s).
#include "ColourScript.h"


// Registers CColourAnalyser.
void registerColourAnalyser(lua_State *L)
{
    // Register low level functions.
    lua_register(L, "caUpdateImage",     lua_ca_updateImage);
    lua_register(L, "caAnalyseImage",    lua_ca_analyseImage);

	lua_register(L, "caConnect",         lua_ca_connect);

    lua_register(L, "caGetIsTarget",     lua_ca_getIsTarget);
    lua_register(L, "caGetIsMatchRGB",   lua_ca_getIsMatchRGB);
    lua_register(L, "caGetIsMatchR",     lua_ca_getIsMatchR);
    lua_register(L, "caGetIsMatchG",     lua_ca_getIsMatchG);
    lua_register(L, "caGetIsMatchB",     lua_ca_getIsMatchB);

    lua_register(L, "caGetTargetR",      lua_ca_getTargetR);
    lua_register(L, "caGetTargetG",      lua_ca_getTargetG);
    lua_register(L, "caGetTargetB",      lua_ca_getTargetB);

    lua_register(L, "caGetTargetMargin", lua_ca_getTargetMargin);
    lua_register(L, "caGetTargetBorder", lua_ca_getTargetBorder);

    lua_register(L, "caGetBiggestR",     lua_ca_getBiggestR);
    lua_register(L, "caGetBiggestG",     lua_ca_getBiggestG);
    lua_register(L, "caGetBiggestB",     lua_ca_getBiggestB);

	lua_register(L, "caGetSelectedDevice", lua_ca_getSelectedDevice);
	lua_register(L, "caGetDevices",      lua_ca_getDevices);

    lua_register(L, "caSetIsTarget",     lua_ca_setIsTarget);
    lua_register(L, "caSetTarget",       lua_ca_setTarget);
    lua_register(L, "caSetTargetR",      lua_ca_setTargetR);
    lua_register(L, "caSetTargetG",      lua_ca_setTargetG);
    lua_register(L, "caSetTargetB",      lua_ca_setTargetB);

    lua_register(L, "caSetTargetMargin", lua_ca_setTargetMargin);
    lua_register(L, "caSetTargetBorder", lua_ca_setTargetBorder);

    // Register the CColourAnalyser class.
    char buffer[] =
    {
       "CColourAnalyser = {}\n" \
       "CColourAnalyser.__index = CColourAnalyser\n" \
       "\n" \
       "function CColourAnalyser.create()\n" \
       "   local handle = {}\n" \
       "   setmetatable(handle, CColourAnalyser)\n" \
       "   return handle\n" \
       "end\n" \
       "\n" \
       "function CColourAnalyser:updateImage() caUpdateImage() end\n" \
       "function CColourAnalyser:analyseImage() caAnalyseImage() end\n" \
       "\n" \
       "function CColourAnalyser:connect() caConnect() end\n" \
	   "\n" \
       "function CColourAnalyser:getIsTarget() return caGetIsTarget() end\n" \
       "function CColourAnalyser:getIsMatchRGB() return caGetIsMatchRGB() end\n" \
       "function CColourAnalyser:getIsMatchR() return caGetIsMatchR() end\n" \
       "function CColourAnalyser:getIsMatchG() return caGetIsMatchG() end\n" \
       "function CColourAnalyser:getIsMatchB() return caGetIsMatchB() end\n" \
       "\n" \
       "function CColourAnalyser:getTargetR() return caGetTargetR() end\n" \
       "function CColourAnalyser:getTargetG() return caGetTargetG() end\n" \
       "function CColourAnalyser:getTargetB() return caGetTargetB() end\n" \
       "\n" \
       "function CColourAnalyser:getTargetMargin() return caGetTargetMargin() end\n" \
       "function CColourAnalyser:getTargetBorder() return caGetTargetBorder() end\n" \
       "\n" \
       "function CColourAnalyser:getBiggestR() return caGetBiggestR() end\n" \
       "function CColourAnalyser:getBiggestG() return caGetBiggestG() end\n" \
       "function CColourAnalyser:getBiggestB() return caGetBiggestB() end\n" \
       "\n" \
	   "function CColourAnalyser:getSelectedDevice() return caGetSelectedDevice() end\n" \
	   "function CColourAnalyser:getDevices() return caGetDevices() end\n" \
	   "\n" \
       "function CColourAnalyser:setIsTarget(value) caSetIsTarget(value) end\n" \
       "function CColourAnalyser:setTarget(R, G, B) caSetTarget(R, G, B) end\n" \
       "function CColourAnalyser:setTargetR(value) caSetTargetR(value) end\n" \
       "function CColourAnalyser:setTargetG(value) caSetTargetG(value) end\n" \
       "function CColourAnalyser:setTargetB(value) caSetTargetB(value) end\n" \
       "\n" \
       "function CColourAnalyser:setTargetMargin(value) caSetTargetMargin(value) end\n" \
       "function CColourAnalyser:setTargetBorder(value) caSetTargetBorder(value) end\n" \
       "\n" \
       "ColourAnalyser = CColourAnalyser.create()"
    };

    luaL_dostring(L, buffer);
}

// Glue functions:
// Glue function for CColourAnalyser::updateImage
int lua_ca_updateImage(lua_State *L)
{
   CColourAnalyser::Instance()->updateImage();
   return 0;
}

// Glue function for CColourAnalyser::analyseImage
int lua_ca_analyseImage(lua_State *L)
{
   CColourAnalyser::Instance()->analyseImage();
   return 0;
}

// Glue function for CColourAnalyser::connect
int lua_ca_connect(lua_State *L)
{
   int lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = (int)lua_tonumber(L, 1);

   CColourAnalyser::Instance()->connect(lua_var);

   return 0;
}

// Glue function for CColourAnalyser::getIsTarget.
int lua_ca_getIsTarget(lua_State *L)
{
   lua_pushboolean(L, CColourAnalyser::Instance()->getIsTarget());
   return 1;
}

// Glue function for CColourAnalyser::getIsMatchRGB.
int lua_ca_getIsMatchRGB(lua_State *L)
{
   lua_pushboolean(L, CColourAnalyser::Instance()->getIsMatchRGB());
   return 1;
}

// Glue function for CColourAnalyser::getIsMatchR.
int lua_ca_getIsMatchR(lua_State *L)
{
   lua_pushboolean(L, CColourAnalyser::Instance()->getIsMatchR());
   return 1;
}

// Glue function for CColourAnalyser::getIsMatchG.
int lua_ca_getIsMatchG(lua_State *L)
{
   lua_pushboolean(L, CColourAnalyser::Instance()->getIsMatchG());
   return 1;
}

// Glue function for CColourAnalyser::getIsMatchB.
int lua_ca_getIsMatchB(lua_State *L)
{
   lua_pushboolean(L, CColourAnalyser::Instance()->getIsMatchB());
   return 1;
}

// Glue function for CColourAnalyser::getTargetR.
int lua_ca_getTargetR(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getTargetR());
   return 1;
}

// Glue function for CColourAnalyser::getTargetG.
int lua_ca_getTargetG(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getTargetG());
   return 1;
}

// Glue function for CColourAnalyser::getTargetB.
int lua_ca_getTargetB(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getTargetB());
   return 1;
}

// Glue function for CColourAnalyser::getTargetMargin.
int lua_ca_getTargetMargin(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getTargetMargin());
   return 1;
}

// Glue function for CColourAnalyser::getTargetBorder.
int lua_ca_getTargetBorder(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getTargetBorder());
   return 1;
}

// Glue function for CColourAnalyser::getBiggestR
int lua_ca_getBiggestR(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getBiggestR());
   return 1;
}

// Glue function for CColourAnalyser::getBiggestG
int lua_ca_getBiggestG(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getBiggestG());
   return 1;
}

// Glue function for CColourAnalyser::getBiggestB
int lua_ca_getBiggestB(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getBiggestB());
   return 1;
}

// Glue function for CColourAnalyser::getSelectedDevice
int lua_ca_getSelectedDevice(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getSelectedDevice());
   return 1;
}

// Glue function for CColourAnalyser::getDevices
int lua_ca_getDevices(lua_State *L)
{
   lua_pushnumber(L, CColourAnalyser::Instance()->getDevices());
   return 1;
}

// Glue function for CColourAnalyser::setIsTarget
int lua_ca_setIsTarget(lua_State *L)
{
   bool lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = (bool)lua_toboolean(L, 1);

   CColourAnalyser::Instance()->setIsTarget(lua_var);

   return 0;
}

// Glue function for CColourAnalyser::setTarget R/G/B.
int lua_ca_setTarget(lua_State *L)
{
   int R, G, B;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   R = (int)lua_tonumber(L, 1);
   if(lua_argnum >= 2)   G = (int)lua_tonumber(L, 2);
   if(lua_argnum >= 3)   B = (int)lua_tonumber(L, 3);

   CColourAnalyser::Instance()->setTargetR(R);
   CColourAnalyser::Instance()->setTargetG(G);
   CColourAnalyser::Instance()->setTargetB(B);

   return 0;
}

// Glue function for CColourAnalyser::setTargetR.
int lua_ca_setTargetR(lua_State *L)
{
   int lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = (int)lua_tonumber(L, 1);

   CColourAnalyser::Instance()->setTargetR(lua_var);

   return 0;
}

// Glue function for CColourAnalyser::setTargetG.
int lua_ca_setTargetG(lua_State *L)
{
   int lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = (int)lua_tonumber(L, 1);

   CColourAnalyser::Instance()->setTargetG(lua_var);

   return 0;
}

// Glue function for CColourAnalyser::setTargetB.
int lua_ca_setTargetB(lua_State *L)
{
   int lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = (int)lua_tonumber(L, 1);

   CColourAnalyser::Instance()->setTargetB(lua_var);

   return 0;
}

// Glue function for CColourAnalyser::setTargetMargin.
int lua_ca_setTargetMargin(lua_State *L)
{
   int lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = (int)lua_tonumber(L, 1);

   CColourAnalyser::Instance()->setTargetMargin(lua_var);

   return 0;
}

// Glue function for CColourAnalyser::setTargetBorder
int lua_ca_setTargetBorder(lua_State *L)
{
   int lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = (int)lua_tonumber(L, 1);

   CColourAnalyser::Instance()->setTargetBorder(lua_var);

   return 0;
}
// End of File.
